package com.hxjb.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;


/**
 * Created by wt on 2017/11/21.
 */
@Aspect
@Component
public class LogSpringActionHandler {
    private static Logger logger = LoggerFactory.getLogger(LogSpringActionHandler.class);

    @Before("execution(public * com.hxjb.controller.*.*(..))")
    public void printActionMsg(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Map map = request.getParameterMap();
        StringBuilder sb = new StringBuilder();
        Iterator iterator = map.keySet().iterator();

        while(iterator.hasNext()) {
            Object obj = iterator.next();
            sb.append(obj).append("=").append(request.getParameter(obj.toString())).append("&");
        }

        logger.info(request.getRequestURL().toString() + "?" + sb.toString());
    }
}
